﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using Model;
using System.Data;
using System.Data.SqlClient;
using Word = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;

//这个类是负责将word中的查找替换题来做的,各个试卷都可以用

namespace NCRE学生考试端V1._0
{
    public class WordAFindKeyWord
    {
        private static  Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
        private static  object unknow = Type.Missing;
        private static object file = @"D:\" + MyInfo.MystudentID() + @"\计算机一级考生文件\" + MyInfo.MyPaperType().Trim() + "卷\\Wordkt\\Word" + MyInfo.MyPaperType().Trim() + ".docx";
        private readonly  Microsoft.Office.Interop.Word.Document doc = app.Documents.Add(ref file, ref unknow, ref unknow, ref unknow);

        private static WordQuestionEntityBll WordQuestion = new WordQuestionEntityBll();
        //根据试卷类型查找题型     
        readonly WordLoadinfo gonggong = new WordLoadinfo();

        #region  王荣晓 查找替换  修改人 ：李少然 2015年12月21日
        /// <summary>
        /// 查找替换
        /// </summary>
        /// <param name="studentinfo"></param>
        public void FindReplaceWord(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {

            DataRow[] drs_word = dt.Select("QuestionFlag like '%查找替换%'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            //传递考生ID进studentrecord实体
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            studentrecord.StudentID = FrmLogin.studentID;

            if (doc.Content.Text.Trim().Length >= Para_StringStartIndex + Para_StringLength)
            {
                //将考生答案传递给studentrecord实体
                studentrecord.ExamAnswer = doc.Content.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
               
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }

            //将试题的ID选择出来
            if (drs_word.Count() == 0)
            {
                return;
            }
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
        }
        #endregion

        #region 王荣晓 插入空行  修改人：程金鹏-2018年4月17日15:48:14
        /// <summary>
        /// 插入空行
        /// </summary>
        /// <param name="studentinfo"></param>
        public void FindKeyWord(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_Paragraphs, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {
            DataRow[] drs_word = dt.Select("QuestionFlag like '%插入空行%'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            studentrecord.TimeStamp = MyInfo.TimeTamp();

            //传递考生ID进studentrecord实体
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            studentrecord.StudentID = FrmLogin.studentID;


            //将考生答案传递给studentrecord实体
            if (doc.Paragraphs.Count >= Para_Paragraphs)//因为判断第para_paragraphs段的信息，所以必须判断是否有第第para_paragraphs段的信息
            {
                //必须判断是否有第第para_paragraphs段的信息是空行吗。
                if (doc.Paragraphs[Para_Paragraphs].Range.Text.Trim() == "\r" || doc.Paragraphs[Para_Paragraphs].Range.Text.Trim().ToString() == "")
                {
                    studentrecord.ExamAnswer = "空答案";
                }
                else//第para_paragraphs段的信息如果不是空行，则判断所要此段信息长度是否大于等于所要截取的长度。
                {
                    if (doc.Paragraphs[Para_Paragraphs].Range.Text.Trim().Length >= Para_StringLength)
                    {
                        studentrecord.ExamAnswer = doc.Paragraphs[Para_Paragraphs].Range.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
                    }
                    else
                    {
                        studentrecord.ExamAnswer = "空答案";
                    }
                }
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";

            }

            //将试题的ID选择出来
            if (drs_word.Count() == 0)
            {
                return;
            }

            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));

        }
        #endregion

        #region  李少然 查找字体 修改人：于云秀 2018年4月24日11:18:22
        /// <summary>
        /// 查找字体
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindFontShapeG(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_Pargraphs, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {
            DataRow[] drs_word = dt.Select("QuestionFlag='查找字体'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            //传递考生ID进studentrecord实体
            studentrecord.StudentID = FrmLogin.studentID;
            if (doc.Paragraphs.Count >= Para_Pargraphs)
            {
                if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim() == "\r" || doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().ToString() == "")
                {
                    studentrecord.ExamAnswer = "空答案";
                }
                else
                {
                    if (doc.Paragraphs[2].Range.Text.Trim().Length >= Para_Pargraphs)
                    {
                        //将考生答案传递给studentrecord实体
                        studentrecord.ExamAnswer = doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
                    }
                    else
                    {
                        studentrecord.ExamAnswer = "空答案";
                    }
                }
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }
            //将试题的ID选择出来
            if (drs_word.Count() == 0)
            {
                return;
            }
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
        }
        #endregion        

        #region  王荣晓 替换段落  修改人：于云秀  2018年4月24日08:19:06
        /// <summary>
        /// 替换段落
        /// </summary>
        /// <param name="studentinfo"></param>
        public void ReplaceParagraphWordC(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {

            DataRow[] drs_word = dt.Select("QuestionFlag='替换段落'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            //传递考生ID进studentrecord实体
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            studentrecord.StudentID = FrmLogin.studentID;
            if (doc.Content.Text.Trim().Length >= Para_StringStartIndex + Para_StringLength)
            {
                //将考生答案传递给studentrecord实体
                studentrecord.ExamAnswer = doc.Content.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }
            //将试题的ID选择出来
            if (drs_word.Count() == 0)
            {
                return;
            }
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
        }
        #endregion

        #region  王荣晓 替换文字 修改人：于云秀 2018年4月24日08:21:26
        /// <summary>
        /// 替换文字
        /// </summary>
        /// <param name="studentinfo"></param>
        public void FindKeyWordC(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {
            DataRow[] drs_word = dt.Select("QuestionFlag='替换文字'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            //传递考生ID进studentrecord实体
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            studentrecord.StudentID = FrmLogin.studentID;
            if (doc.Content.Text.Trim().Length >= Para_StringStartIndex + Para_StringLength)
            {
                //将考生答案传递给studentrecord实体
                studentrecord.ExamAnswer = doc.Content.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }
            //将试题的ID选择出来
            if (drs_word.Count() == 0)
            {
                return;
            }
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
        }
        #endregion

        #region  李少然 查找括号  修改人：于云秀 2018年4月24日08:21:26
        /// <summary>
        /// 查找括号
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindChinesebracketsE(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_Pargraphs, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {
            DataRow[] drs_word = dt.Select("QuestionFlag='查找括号'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            //传递考生ID进studentrecord实体
            //传递时间戳进studentrecord实体
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            studentrecord.StudentID = FrmLogin.studentID;
            //将考生答案传递给studentrecord实体
            if (doc.Paragraphs.Count >= Para_Pargraphs)
            {
                if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim() == "\r" || doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().ToString() == "")
                {
                    studentrecord.ExamAnswer = "空答案";
                }
                else
                {
                    if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().Length >= Para_StringStartIndex + Para_StringLength)
                    {
                        studentrecord.ExamAnswer = doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
                    }
                    else
                    {
                        studentrecord.ExamAnswer = "空答案";
                    }
                }
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }
            //将试题的ID选择出来
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
        }
        #endregion

        #region  李少然 查找空格 修改人：于云秀 2018年4月24日08:21:26
        /// <summary>
        /// 查找空格
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindSearchSpacesE(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_Pargraphs, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {
            DataRow[] drs_word = dt.Select("QuestionFlag='查找空格'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            //传递考生ID进studentrecord实体
            //传递时间戳进studentrecord实体
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            studentrecord.StudentID = FrmLogin.studentID;
            //将考生答案传递给studentrecord实体
            if (doc.Paragraphs.Count >= Para_Pargraphs)
            {
                if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim() == "\r" || doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().ToString() == "")
                {
                    studentrecord.ExamAnswer = "空答案";
                }
                else
                {
                    if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().Length >= Para_StringStartIndex + Para_StringLength)
                    {
                        studentrecord.ExamAnswer = doc.Paragraphs[6].Range.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);

                    }
                    else
                    {
                        studentrecord.ExamAnswer = "空答案";
                    }
                }
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }
            //将试题的ID选择出来
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
        }
        #endregion

        #region  李少然 查找空行  修改人：于云秀 2018年4月24日08:21:26
        /// <summary>
        /// 查找空行
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindLineE(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_Pargraphs, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {
            DataRow[] drs_word = dt.Select("QuestionFlag='查找空行'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            //传递考生ID进studentrecord实体
            //传递时间戳进studentrecord实体
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            studentrecord.StudentID = FrmLogin.studentID;
            //将考生答案传递给studentrecord实体
            if (doc.Paragraphs.Count >= Para_Pargraphs)
            {
                if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim() == "\r" || doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().ToString() == "")
                {
                    studentrecord.ExamAnswer = "空答案";
                }
                else
                {
                    if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().Length >= Para_StringLength)
                    {
                        studentrecord.ExamAnswer = doc.Paragraphs[1].Range.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
                    }
                    else
                    {
                        studentrecord.ExamAnswer = "空答案";
                    }

                }//将试题的ID选择出来
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }
            if (drs_word.Count() == 0)
            {
                return;
            }
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
        }
        #endregion

        #region  李少然 图片宽高  修改人：于云秀 2018年4月24日08:21:26
        /// <summary>:10
        /// 图片宽高
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindPictureWidthE(WordQuestionEntity wordinfo, DataTable dt)
        {
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            DataRow[] drs_word = dt.Select("QuestionFlag='图片宽高'");

            if (doc.Shapes.Count == 0)
            {
                //如果表格不存在，则判分为0
                //传递时间戳进studentrecord实体
                studentrecord.TimeStamp = MyInfo.TimeTamp();
                studentrecord.StudentID = FrmLogin.studentID;
                studentrecord.ExamAnswer = "0";
                studentrecord.Fration = "0";
                //将答题记录送到数据库
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
                }
                WordLoadinfo.list.Add(studentrecord);

            }
            else
            {
                //传递考生ID进studentrecord实体
                studentrecord.StudentID = FrmLogin.studentID;
                //传递时间戳进studentrecord实体
                studentrecord.TimeStamp = MyInfo.TimeTamp();
                //将考生答案传递给studentrecord实体
                studentrecord.ExamAnswer = doc.Shapes[1].Height.ToString() + doc.Shapes[1].Width.ToString();
                //将试题的ID选择出来
                if (drs_word.Count() == 0)
                {
                    return;
                }
                studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
                //判分
                WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
            }

        }
        #endregion

        #region  李少然 插入图片  修改人：于云秀 2018年4月24日08:21:26
        /// 查找图片
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindPictureF(WordQuestionEntity wordinfo, DataTable dt)
        {

            DataRow[] drs_word = dt.Select("QuestionFlag='插入图片'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            //传递考生ID进studentrecord实体
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            studentrecord.StudentID = FrmLogin.studentID;
            //将考生答案传递给studentrecord实体
            studentrecord.ExamAnswer = doc.Shapes.Count.ToString();
            //将试题的ID选择出来
            if (drs_word.Count() == 0)
            {
                return;
            }
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));

        }
        #endregion

        #region  李少然 图片宽度  修改人：于云秀 2018年4月24日08:21:26
        /// <summary>:10
        /// 图片宽高
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindPictureWidthF(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_PictureID)
        {
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();

            DataRow[] drs_word = dt.Select("QuestionFlag='图片宽度'");
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            if (doc.Shapes.Count == 0)
            {
                //如果表格不存在，则判分为0
                studentrecord.StudentID = FrmLogin.studentID;
                studentrecord.ExamAnswer = "0";
                studentrecord.Fration = "0";
                //将答题记录送到数据库
                if (drs_word.Count() == 0)
                {
                    return;
                }
                studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);

                WordLoadinfo.list.Add(studentrecord);

            }
            else
            {
                //传递考生ID进studentrecord实体
                studentrecord.StudentID = FrmLogin.studentID;


                //将考生答案传递给studentrecord实体
                studentrecord.ExamAnswer = doc.Shapes[Para_PictureID].Height.ToString() + doc.Shapes[Para_PictureID].Width.ToString();
                //将试题的ID选择出来
                if (drs_word.Count() == 0)
                {
                    return;
                }
                studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
                //判分
                WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
            }



        }
        #endregion

        #region  李少然 查找文字   修改人：于云秀 2018年4月24日08:21:26
        /// <summary>
        /// 查找文字
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindFontShapeH(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_Pargraphs, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {
            DataRow[] drs_word = dt.Select("QuestionFlag='查找文字'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            //传递考生ID进studentrecord实体
            studentrecord.StudentID = FrmLogin.studentID;
            if (doc.Paragraphs.Count >= Para_Pargraphs)
            {
                if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim() == "\r" || doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().ToString() == "")
                {
                    studentrecord.ExamAnswer = "空答案";
                }
                else
                {
                    if (doc.Paragraphs[1].Range.Text.Trim().Length >= 4)
                    {
                        //将考生答案传递给studentrecord实体
                        studentrecord.ExamAnswer = doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
                    }
                    else
                    {
                        studentrecord.ExamAnswer = "空答案";
                    }
                }
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }
            //将试题的ID选择出来
            if (drs_word.Count() == 0)
            {
                return;
            }
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));
        }
        #endregion

        #region  李少然 查找标点  修改人：于云秀 2018年4月24日08:21:26
        /// <summary>
        /// 查找标点
        /// </summary>
        /// <param name="wordinfo">题库实体</param>
        public void FindPunctuationH(WordQuestionEntity wordinfo, DataTable dt, Int32 Para_Pargraphs, Int32 Para_StringStartIndex, Int32 Para_StringLength)
        {
            DataRow[] drs_word = dt.Select("QuestionFlag='查找标点'");
            WordQuestionRecordEntity studentrecord = new WordQuestionRecordEntity();
            studentrecord.TimeStamp = MyInfo.TimeTamp();
            //传递考生ID进studentrecord实体
            studentrecord.StudentID = FrmLogin.studentID;
            if (doc.Paragraphs.Count >= Para_Pargraphs)
            {
                if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim() == "\r" || doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().ToString() == "")
                {
                    studentrecord.ExamAnswer = "空答案";
                }
                else
                {
                    if (doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().Length >= Para_StringLength)
                    {
                        //将考生答案传递给studentrecord实体
                        studentrecord.ExamAnswer = doc.Paragraphs[Para_Pargraphs].Range.Text.Trim().Substring(Para_StringStartIndex, Para_StringLength);
                    }
                    else
                    {
                        studentrecord.ExamAnswer = "空答案";
                    }
                }
            }
            else
            {
                studentrecord.ExamAnswer = "空答案";
            }

            //将试题的ID选择出来
            if (drs_word.Count() == 0)
            {
                return;
            }
            studentrecord.QuestionID = Convert.ToDouble(drs_word[0]["QuestionID"]);
            //判分         
            WordLoadinfo.list.Add(gonggong.getStudentRecord(drs_word, studentrecord));

        }
        #endregion


    }
}
